<?php

namespace App\Admin\Controllers\Posts;

use App\Admin\Repositories\Posts\WxTag;
use App\Wen\Utils\Settings;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class WxTagController extends AdminController
{
    public function tags(Request $request)
    {
        $q = $request->get('q');
        return \App\Models\Posts\WxTag::where('tags_name', 'like', "%$q%")->paginate(null, ['id', 'tags_name as text']);
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxTag(), function (Grid $grid) {
            $grid->simplePaginate();
            $grid->model()->orderBy('id', 'desc');
            $grid->quickSearch(['id', 'tags_name', 'tag_introduce'])->placeholder('搜索话题ID，名字，简介...');
            $grid->model()->orderBy('id','desc');
            $grid->column('id')->sortable();
            $grid->column('head_portrait')->image('',50);
            $grid->column('tags_name');
            $grid->column('tag_introduce');
            $grid->column('sort')->editable();
            $grid->column('tags_number')->display(function ($v){
                if($v === null){
                    \App\Models\Posts\WxTag::whereNull('tags_number')->update(['tags_number'=>0]);
                }
                return $v;
            });
            $grid->column('tags_state')->display(function ($v){
                if($v === null){
                    \App\Models\Posts\WxTag::whereNull('tags_state')->update( ['tags_state' => 0] );
                }
                return $v;
            })->using([0=>'正常',1=>'隐藏'])->label([0=>'primary', 1=>'default']);
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('tags_name');
                $filter->equal('tags_state')->select([0=>'正常',1=>'隐藏']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxTag(), function (Show $show) {
            $show->field('id');
            $show->field('tags_name');
            $show->field('tag_introduce');
            $show->field('head_portrait');
            $show->field('background_maps');
            $show->field('pc_background_maps');
            $show->field('tags_number');
            $show->field('tags_state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxTag(), function (Form $form) {
            $form->display('id');
            $form->text('tags_name')->required();
            $form->textarea('tag_introduce')->required();
            $form->image('head_portrait')->url('files/uploads')->uniqueName()->autoUpload()->required()->default(Settings::get('img_default_topic_avatar', 'https://img.mini.minisns.cn/images/topci/avatar.png'));
            $form->image('background_maps')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('img_default_topic_bg', ''));
            $form->image('pc_background_maps')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('img_default_topic_pc_bg', ''));
            $form->number('sort')->default(0);
            $form->text('tags_number')->default(0);
            $form->radio('tags_state', '是否隐藏')->options([0=>'正常', 1=>'管理员隐藏'])->default(0);

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
